package com.neudu.ssrs_yyzxservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.neudu.ssrs_yyzxservice.common.JSONReturn;
import com.neudu.ssrs_yyzxservice.pojo.Nursecontent;
import com.neudu.ssrs_yyzxservice.service.NursecontentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class NursecontentController {

    @Autowired
    private NursecontentService nursecontentService;
    @Autowired
    private JSONReturn jsonReturn;

    // 获取所有护理项目信息
    @PostMapping("/get_all_contents")
    public String getAllContents() {
        try {
            List<Nursecontent> contents = nursecontentService.list();
            return jsonReturn.returnSuccess(contents);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据护理项目名称模糊查询
    @PostMapping("/get_contents_by_name")
    public String getContentsByName(@RequestBody Nursecontent nursecontent) {
        try {
            LambdaQueryWrapper<Nursecontent> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(ObjectUtils.isEmpty(nursecontent.getNursingName()), Nursecontent::getNursingName, nursecontent.getNursingName());
            List<Nursecontent> contents = nursecontentService.list(queryWrapper);
            return jsonReturn.returnSuccess(contents);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据ID获取护理项目信息
    @PostMapping("/get_content_by_id")
    public String getContentById(@RequestBody Nursecontent nursecontent) {
        try {
            Nursecontent content = nursecontentService.getById(nursecontent.getId());
            return jsonReturn.returnSuccess(content);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 添加护理项目
    @PostMapping("/add_content")
    public String addContent(@RequestBody Nursecontent nursecontent) {
        try {
            nursecontentService.save(nursecontent);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 修改护理项目
    @PostMapping("/modify_content")
    public String modifyContent(@RequestBody Nursecontent nursecontent) {
        try {
            nursecontentService.updateById(nursecontent);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 删除护理项目
    @PostMapping("/remove_content")
    public String removeContent(@RequestBody Nursecontent nursecontent) {
        try {
            nursecontentService.removeById(nursecontent.getId());
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}